package com.jmb.waimao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmb.waimao.entity.EmailTemplate;
import com.jmb.waimao.mapper.EmailTemplateMapper;
import com.jmb.waimao.service.EmailTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Implementation of EmailTemplateService
 */
@Service
@Slf4j
public class EmailTemplateServiceImpl extends ServiceImpl<EmailTemplateMapper, EmailTemplate> implements EmailTemplateService {

    private static final Pattern VARIABLE_PATTERN = Pattern.compile("\\{\\{(.*?)\\}\\}");

    @Override
    public EmailTemplate createTemplate(EmailTemplate template) {
        LocalDateTime now = LocalDateTime.now();
        template.setCreateTime(now);
        template.setUpdateTime(now);
        template.setDeleted(0);
        
        save(template);
        
        return template;
    }

    @Override
    public EmailTemplate applyTemplateVariables(Long templateId, Map<String, String> variables) {
        EmailTemplate template = getById(templateId);
        if (template == null) {
            throw new RuntimeException("Template not found: " + templateId);
        }
        
        // Create a copy of the template
        EmailTemplate processedTemplate = new EmailTemplate();
        processedTemplate.setId(template.getId());
        processedTemplate.setTemplateName(template.getTemplateName());
        processedTemplate.setDescription(template.getDescription());
        
        // Process subject
        String processedSubject = template.getSubject();
        for (Map.Entry<String, String> entry : variables.entrySet()) {
            processedSubject = processedSubject.replace("{{" + entry.getKey() + "}}", entry.getValue());
        }
        processedTemplate.setSubject(processedSubject);
        
        // Process content
        String processedContent = template.getContent();
        for (Map.Entry<String, String> entry : variables.entrySet()) {
            processedContent = processedContent.replace("{{" + entry.getKey() + "}}", entry.getValue());
        }
        processedTemplate.setContent(processedContent);
        
        return processedTemplate;
    }
    
    /**
     * Check if a template has all required variables
     * 
     * @param template Template to check
     * @return Map of missing variables
     */
    public Map<String, Boolean> checkTemplateVariables(EmailTemplate template) {
        Map<String, Boolean> variables = new java.util.HashMap<>();
        
        // Check subject
        Matcher subjectMatcher = VARIABLE_PATTERN.matcher(template.getSubject());
        while (subjectMatcher.find()) {
            variables.put(subjectMatcher.group(1), false);
        }
        
        // Check content
        Matcher contentMatcher = VARIABLE_PATTERN.matcher(template.getContent());
        while (contentMatcher.find()) {
            variables.put(contentMatcher.group(1), false);
        }
        
        return variables;
    }
}
